package com.ruoyi.liangshi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.liangshi.domain.BizKcKsc;
import com.ruoyi.liangshi.mapper.BizBsOrderMapper;
import com.ruoyi.liangshi.mapper.BizKcKscMapper;
import com.ruoyi.liangshi.service.IBizKcKscService;
import com.ruoyi.liangshi.vo.query.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 门店营业收银订单菜品上菜信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-18
 */
@Service
public class BizKcKscServiceImpl extends ServiceImpl<BizKcKscMapper, BizKcKsc> implements IBizKcKscService {
    @Autowired
    private BizKcKscMapper bizKcKscMapper;
    @Value("${arch-item.excluds}")
    private String archItemExcluds;

    @Autowired
    private BizBsOrderMapper bizBsOrderMapper;

    @Override
    public BiBsTimeOutDataVo getTimeOutDishesData(OrderAndKscCommonQueryVo orderAndKscCommonQueryVo) {
        List<String> excludeItemIds = Arrays.asList(archItemExcluds.split(","));
        orderAndKscCommonQueryVo.setExcludeItemIds(excludeItemIds);
        return bizKcKscMapper.getTimeOutDishesData(orderAndKscCommonQueryVo);
    }

    @Override
    public List<BiKscMealTrendChartDataVo> getMealTrendChartData(OrderAndKscCommonQueryVo orderAndKscCommonQueryVo) {
        List<String> excludeItemIds = Arrays.asList(archItemExcluds.split(","));
        orderAndKscCommonQueryVo.setExcludeItemIds(excludeItemIds);
        return bizKcKscMapper.getMealTrendChartData(orderAndKscCommonQueryVo);
    }

    @Override
    public List<BiMealStatDataVo> getMealStatData(OrderAndKscCommonQueryVo orderAndKscCommonQueryVo) {
        List<String> excludeItemIds = Arrays.asList(archItemExcluds.split(","));
        orderAndKscCommonQueryVo.setExcludeItemIds(excludeItemIds);
        //获取菜品数量信息
        List<BiMealStatDataVo> resultStatData = bizKcKscMapper.getMealStatData(orderAndKscCommonQueryVo);
        // 获取每日订单数量信息
        List<BiMealStatDataVo> orderStatData = bizBsOrderMapper.getMealStatData(orderAndKscCommonQueryVo);
        // 合并订单数量和菜品数量
        Map<String, BiMealStatDataVo> maps = orderStatData.stream().collect(Collectors.toMap(BiMealStatDataVo::getDate, Function.identity()));
        for(BiMealStatDataVo item:resultStatData){
            BiMealStatDataVo orderStat = maps.get(item.getDate()) ;
            if(orderStat!=null){
                item.setOrderCount(orderStat.getOrderCount());
                item.setOrderTimeOutCount(orderStat.getOrderTimeOutCount());
                BigDecimal timeOutKsc = item.getKscTimeOutCount();
                BigDecimal totalKsc = item.getKscCount();
                if(totalKsc.compareTo(BigDecimal.ZERO)>0){
                    BigDecimal  timeOutPer = timeOutKsc.divide(totalKsc,4,BigDecimal.ROUND_HALF_UP);
                    item.setKscTimeOutPer(timeOutPer.multiply(new BigDecimal(100)));
                }else{
                    item.setKscTimeOutPer(BigDecimal.ZERO);
                }
                item.setAvgTime(item.getAvgTime().divide(new BigDecimal(60),2,BigDecimal.ROUND_HALF_UP));
                item.setAvgCookTime(item.getAvgCookTime().divide(new BigDecimal(60),2,BigDecimal.ROUND_HALF_UP));
                item.setAvgServeTime(item.getAvgServeTime().divide(new BigDecimal(60),2,BigDecimal.ROUND_HALF_UP));
                item.setConsumeTime(item.getConsumeTime().divide(new BigDecimal(60),2,BigDecimal.ROUND_HALF_UP));

            }
        }
        return resultStatData;
    }

    @Override
    public KscItemTypeDataInfoVo getKscItemTypeDataInfo(OrderAndKscCommonQueryVo orderAndKscCommonQueryVo) {
        List<String> excludeItemIds = Arrays.asList(archItemExcluds.split(","));
        orderAndKscCommonQueryVo.setExcludeItemIds(excludeItemIds);
        KscItemTypeDataInfoVo kscItemTypeDataInfo = bizKcKscMapper.getKscItemTypeDataInfo(orderAndKscCommonQueryVo);
        KscItemTypeDataInfoVo minConsumeTimeMonth = bizKcKscMapper.getMinConsumeTimeMonth(orderAndKscCommonQueryVo);
        kscItemTypeDataInfo.setMinTimeDate(minConsumeTimeMonth.getMinTimeDate());
        kscItemTypeDataInfo.setMinConsumeTime(minConsumeTimeMonth.getMinConsumeTime());
        return kscItemTypeDataInfo;
    }

    @Override
    public List<BizKscItemInfoVo> getKscAndArchItemInfoByDate(Long shopId, String shift, String date, String time) {
        List<String> excludeItemIds = Arrays.asList(archItemExcluds.split(","));
        return bizKcKscMapper.getKscAndArchItemInfoByDate(shopId, shift, date, time, excludeItemIds);
    }

    @Override
    public Map<String, Object> getMonthTimeOutInfo(OrderAndKscCommonQueryVo orderAndKscCommonQueryVo) {
        List<String> excludeItemIds = Arrays.asList(archItemExcluds.split(","));
        orderAndKscCommonQueryVo.setExcludeItemIds(excludeItemIds);
        // 当月的总订单和总超时订单，
        Map<String,Object> orderMap = bizBsOrderMapper.getMonthTimeOutInfo(orderAndKscCommonQueryVo);
        // 当月的总菜和总超时菜以及超时率
        Map<String,Object> kscMap = bizKcKscMapper.getMonthTimeOutInfo(orderAndKscCommonQueryVo);
        orderMap.putAll(kscMap);
        int timeOutOrder = Integer.parseInt(orderMap.get("time_out_order").toString());
        int totalOrder = Integer.valueOf(orderMap.get("total_order").toString());
        if(totalOrder!=0){
            BigDecimal  timeOutPer = new BigDecimal(timeOutOrder).divide(new BigDecimal(totalOrder),4,BigDecimal.ROUND_HALF_UP);
            orderMap.put("orderTimeoutPer",timeOutPer.multiply(new BigDecimal(100)));
        }else{
            orderMap.put("orderTimeoutPer",BigDecimal.ZERO);
        }
        // 计算超时率
        int timeOutKsc = Integer.parseInt(kscMap.get("time_out_ksc").toString());
        int totalKsc = Integer.valueOf(kscMap.get("total_ksc").toString());
        if(totalKsc!=0){
            BigDecimal  timeOutPer = new BigDecimal(timeOutKsc).divide(new BigDecimal(totalKsc),4,BigDecimal.ROUND_HALF_UP);
            orderMap.put("KscTimeoutPer",timeOutPer.multiply(new BigDecimal(100)));
        }else{
            orderMap.put("KscTimeoutPer",BigDecimal.ZERO);
        }
        return orderMap ;
    }

    @Override
    public Map<String, Object> getDayTimeOutInfo(OrderAndKscCommonQueryVo orderAndKscCommonQueryVo) {
        List<String> excludeItemIds = Arrays.asList(archItemExcluds.split(","));
        orderAndKscCommonQueryVo.setExcludeItemIds(excludeItemIds);
        return bizKcKscMapper.getDayTimeOutInfo(orderAndKscCommonQueryVo);
    }

    /**
     * 查询门店营业收银订单菜品上菜信息
     *
     * @param id 门店营业收银订单菜品上菜信息主键
     * @return 门店营业收银订单菜品上菜信息
     */
    @Override
    public BizKcKsc selectBizKcKscById(Long id) {
        return bizKcKscMapper.selectBizKcKscById(id);
    }

    /**
     * 查询门店营业收银订单菜品上菜信息列表
     *
     * @param bizKcKsc 门店营业收银订单菜品上菜信息
     * @return 门店营业收银订单菜品上菜信息
     */
    @Override
    public List<BizKcKsc> selectBizKcKscList(BizKcKsc bizKcKsc) {
        return bizKcKscMapper.selectBizKcKscList(bizKcKsc);
    }

    /**
     * 新增门店营业收银订单菜品上菜信息
     *
     * @param bizKcKsc 门店营业收银订单菜品上菜信息
     * @return 结果
     */
    @Override
    public int insertBizKcKsc(BizKcKsc bizKcKsc) {
        //bizKcKsc.setCreateTime(DateUtils.getNowDate());
        return bizKcKscMapper.insertBizKcKsc(bizKcKsc);
    }

    /**
     * 修改门店营业收银订单菜品上菜信息
     *
     * @param bizKcKsc 门店营业收银订单菜品上菜信息
     * @return 结果
     */
    @Override
    public int updateBizKcKsc(BizKcKsc bizKcKsc) {
        return bizKcKscMapper.updateBizKcKsc(bizKcKsc);
    }

    /**
     * 批量删除门店营业收银订单菜品上菜信息
     *
     * @param ids 需要删除的门店营业收银订单菜品上菜信息主键
     * @return 结果
     */
    @Override
    public int deleteBizKcKscByIds(Long[] ids) {
        return bizKcKscMapper.deleteBizKcKscByIds(ids);
    }

    /**
     * 删除门店营业收银订单菜品上菜信息信息
     *
     * @param id 门店营业收银订单菜品上菜信息主键
     * @return 结果
     */
    @Override
    public int deleteBizKcKscById(Long id) {
        return bizKcKscMapper.deleteBizKcKscById(id);
    }
}
